<?php

/**
 * PointsCalculation form.
 *
 * @package    cpfaizenay
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class PointsCalculationForm extends BaseForm
{
  public function configure() {

      $dateWidget = new sfWidgetFormI18nDate(array(
                    'format' => '%day%/%month%/%year%',
                    //      'month_format' => 'short_name',
                    'culture' => 'fr'
                  ));
      
      $this->widgetSchema['datetime_match'] = new sfWidgetFormJQueryDate(array(
          'culture' => 'fr',
          'date_widget' => $dateWidget,
          'image' => '/images/admin/icons/small/calendar.png',
          'config' => '{ maxDate: new Date() }'
      ));
      $this->widgetSchema['datetime_match']->setDefault(date('d-m-Y'));
      $this->widgetSchema['datetime_match']->setLabel('Date des matchs :');

      $match_type_list = Doctrine::getTable('MatchType')->getMatchType()->fetchArray();
      $choices = array();
      foreach($match_type_list as $match_type){
          $choices[ $match_type[ 'id' ] ] = $match_type[ 'title' ] . ' [' . $match_type[ 'coefficient' ] . ']';
      }
      $this->widgetSchema['match_type'] = new sfWidgetFormSelect(array('choices' => $choices));
      $this->widgetSchema['match_type']->setLabel('Sélectionnez votre compétition :');

      $this->widgetSchema['mensual_points'] = new sfWidgetFormInputText();
      $this->widgetSchema['mensual_points']->setLabel('Vos points mensuels* :');

      $this->widgetSchema['startSeason_mensualPoints'] = new sfWidgetFormInputText();
      $this->widgetSchema['startSeason_mensualPoints']->setLabel('Vos points du début de saison* :');

      $this->setValidators(array(
          'datetime_match'     => new sfValidatorDateTime(array('required' => true)),
          'match_type'         => new sfValidatorDoctrineChoice(array('model' => 'matchType', 'required' => false)),
          'mensual_points'     => new sfValidatorString(array('max_length' => 5, 'required' => false)),
          'startSeason_mensualPoints' => new sfValidatorString(array('max_length' => 5, 'required' => false))
      ));

      $this->validatorSchema->setPostValidator(
            new sfValidatorOr(array(
                    new sfValidatorSchemaFilter('mensual_points', new sfValidatorString(array('max_length' => 5, 'required' => true))),
                    new sfValidatorSchemaFilter('startSeason_mensualPoints', new sfValidatorString(array('max_length' => 5, 'required' => true)))
                )
            )
      );

      $subForm = new sfForm();
      for($i=0; $i<9; $i++){
          $licenseeMatch = new LicenseeMatch();
          $form = new InstanceLicenseeMatchForm($licenseeMatch);
          $subForm->embedForm($i, $form);
      }
      $this->embedForm('licensee_match', $subForm);


      $this->widgetSchema->setNameFormat('points_calculation[%s]');
  }

  public function save($con = null){
      $id_user = sfContext::getInstance()->getUser()->getGuardUser()->getId();
      $member_info = Doctrine::getTable('Licensee')->getMemberUserInfo($id_user)->fetchOne();

      $licensee_match_val = $this->getValue('licensee_match');

      // Enregistrement des matchs
      for($i=0; $i<9; $i++){
          if(!empty($licensee_match_val[$i]['opponent_point'])){
              $licensee_match = new LicenseeMatch();
              $licensee_match->setLicenseeId($member_info['id']);
              $licensee_match->setStatus($licensee_match_val[$i]['status']);
              $licensee_match->setOpponentLicenseeNumber($licensee_match_val[$i]['opponent_licensee_number']);
              $licensee_match->setOpponentFirstname($licensee_match_val[$i]['opponent_firstname']);
              $licensee_match->setOpponentLastname($licensee_match_val[$i]['opponent_lastname']);
              $licensee_match->setOpponentPoint($licensee_match_val[$i]['opponent_point']);

              $licensee_match->setMatchTypeId($this->getValue('match_type'));
              $licensee_match->setDatetimeMatch($this->getValue('datetime_match'));
              $licensee_match->save();
          }
      }

      // Enregistrement des points du début de saison ( août )
      $currentSeason = strtotime($this->getValue('datetime_match'));
      $currentYear = date('m', $currentSeason) > 6 ? date('Y', $currentSeason) : (date('Y', $currentSeason)-1);
      $licenseePointStartSeason = Doctrine::getTable('LicenseePoint')->getLicenseePointFromDatetime($member_info['id'], $currentYear. '-08-01')->fetchOne();

      if(!$licenseePointStartSeason){
          $licenseePointStartSeason = new LicenseePoint();
          $licenseePointStartSeason->setLicenseeId($member_info['id']);
          $licenseePointStartSeason->setCreationUserId($id_user);
          $licenseePointStartSeason->setModificationUserId($id_user);
          $licenseePointStartSeason->setDatetimePoints($currentYear. '-08-01');
      }

      $startSeason_mensualPoints = $this->getValue('startSeason_mensualPoints');
      $licenseePointStartSeason->setNbPoints($startSeason_mensualPoints);
      $licenseePointStartSeason->save();

  }
}